package com.diseaseInformation

import org.springframework.web.multipart.commons.CommonsMultipartFile

class AtlasController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [atlasInstanceList: Atlas.list(params), atlasInstanceTotal: Atlas.count()]
    }

    def create = {
        def atlasInstance = new Atlas()
        atlasInstance.properties = params
        return [atlasInstance: atlasInstance]
    }

    def save = {
        def atlasInstance = new Atlas(params)
        CommonsMultipartFile f = (CommonsMultipartFile)params.image          
      def extension = f.getOriginalFilename().split("\\.")[-1] //Lasted of String Array
        
      switch(extension.toUpperCase()){
        case ["JPG","JPEG", "PNG", "GIF"]:
             if (atlasInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.created.message', args: [message(code: 'atlas.label', default: 'Atlas'), atlasInstance.id])}"
                redirect(action: "show", id: atlasInstance.id)
                return
            }
            else {
                render(view: "create", model: [atlasInstance: atlasInstance])
                return
            }
             break
        default:
             flash.message += "File Extention must in [jpg,jpeg,png,gif]"
             render(view: "create", model: [atlasInstance: atlasInstance])
             return
             break
        }
        
    }

    def show = {
        def atlasInstance = Atlas.get(params.id)
        if (!atlasInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'atlas.label', default: 'Atlas'), params.id])}"
            redirect(action: "list")
        }
        else {
            Atlas.get(params.id).viewer++ //Count User Viewver
            [atlasInstance: atlasInstance]
        }
    }

    def edit = {
        def atlasInstance = Atlas.get(params.id)
        if (!atlasInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'atlas.label', default: 'Atlas'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [atlasInstance: atlasInstance]
        }
    }

    def update = {
        def atlasInstance = Atlas.get(params.id)
        if (atlasInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (atlasInstance.version > version) {
                    
                    atlasInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'atlas.label', default: 'Atlas')] as Object[], "Another user has updated this Atlas while you were editing")
                    render(view: "edit", model: [atlasInstance: atlasInstance])
                    return
                }
            }
            atlasInstance.properties = params
            if (!atlasInstance.hasErrors() && atlasInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'atlas.label', default: 'Atlas'), atlasInstance.id])}"
                redirect(action: "show", id: atlasInstance.id)
            }
            else {
                render(view: "edit", model: [atlasInstance: atlasInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'atlas.label', default: 'Atlas'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def atlasInstance = Atlas.get(params.id)
        if (atlasInstance) {
            try {
                atlasInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'atlas.label', default: 'Atlas'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'atlas.label', default: 'Atlas'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'atlas.label', default: 'Atlas'), params.id])}"
            redirect(action: "list")
        }
    }
    
    def image= {
        def atlas = Atlas.get( params.id )
        byte[] image = atlas.image
        response.outputStream << image
    }
}
